﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WL_Store.BLL;
using WL_Store.Model;

namespace WL_Store.Web.subpage
{
    public partial class Feedback : System.Web.UI.Page
    {
        FeedbackBLL bll = new FeedbackBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               BindList();
                BindRpt();
            }
        }

        private void BindList()
        {
            DataTable dt = bll.GetTypes();
            ddl_type.DataSource = dt;
            ddl_type.DataTextField = "type";
            ddl_type.DataValueField = "typeId";
            ddl_type.DataBind();
            ddl_type.Items.Insert(0,new ListItem("全部","0"));

            ddl_add_type.DataSource = dt;
            ddl_add_type.DataTextField = "type";
            ddl_add_type.DataValueField = "typeId";
            ddl_add_type.DataBind();
        }

        private void BindRpt()
        {
            string name = txt_search_name.Text.Trim();
            DateTime? startFeedtime = null;
            if (!string.IsNullOrWhiteSpace(txt_StartFeedtime.Value.Trim()))
            {
                startFeedtime = Convert.ToDateTime(txt_StartFeedtime.Value.Trim());
            }
            DateTime? endFeedtime = null;
            if (!string.IsNullOrWhiteSpace(txt_EndFeedtime.Value.Trim()))
            {
                endFeedtime = Convert.ToDateTime(txt_EndFeedtime.Value.Trim());
            }
           int typeId = Convert.ToInt32(ddl_type.SelectedValue);



            FeedbackSearchModel searchModel = new FeedbackSearchModel()
            { 
            name=name,
            StartFeedtime=startFeedtime,
            EndFeedtime=endFeedtime,
            typeId=typeId
            };

           List<FeedbackModel> feedbacks= bll.GetFeedbacks(searchModel);

            rpt_feedbacks.DataSource = feedbacks;
            rpt_feedbacks.DataBind();
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            BindRpt();
        }

        protected void lbt_del_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName=="del")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                if (bll.DeleteFeedback(id))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除成功！');</script>");
                    BindRpt();
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除失败！');</script>");
                }

            }
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //获取用户输入的值
                string number = txt_add_number.Text.Trim();
                string name = txt_add_name.Text.Trim();
                string count = txt_add_count.Text.Trim();
                string feedtime = txt_add_time.Value.Trim();
                int  type = Convert.ToInt32(ddl_add_type.SelectedValue);

                FeedbackModel model = new FeedbackModel()
                {
                    
                    number=number,
                    name=name,
                    feedCount=count,
                    feedTime=Convert.ToDateTime(feedtime),
                    typeId=type,
                    state=Convert.ToBoolean(false),
                    isDelete = Convert.ToBoolean(false)
                };
                if (bll.AddFeedback(model))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(),"alert","<script>alert('添加成功！');location.href='Feedback.aspx'</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加失败！');</script>");
                }

            }
        }

        protected void btn_clear_Click(object sender, EventArgs e)
        {

        }
    }
}